/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui.cd;

import java.awt.Color;
import java.util.Map;

import javax.swing.table.AbstractTableModel;

import org.apache.log4j.Logger;

import who.tmap.bo.cd.ColorPatternRange;
import who.tmap.bo.cd.ColorPatternClassification;
import who.tmap.services.ColorService;
import who.tmap.services.TextureService;

/**
 * @author stl
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class RangeTableModel extends AbstractTableModel {
		
    private static Logger logger = Logger.getLogger(RangeTableModel.class); 
	
	private static final TextureService textureHelper =TextureService.getInstance();
	
	public static final int CAPTION_COL =0;
	public static final int FROM_COL =1;
	public static final int TO_COL =2;	
	public static final int COLOR_COL =3;
	public static final int FILL_PATTERN_COL =4;
	
	
	private String[] columnNames = {"Caption", "From", "To", "Color", "Pattern"  };
	
	private Map<Integer, ColorPatternRange> ranges;	
	private ColorService[] colorServices;
	private int classification;
	private boolean dirty =false;	
	
	public boolean isDirty() {
		return dirty;
	}

	public void setDirty(boolean dirty) {
		this.dirty = dirty;
	}

	public void setClassification( int classification ) {
		this.classification =classification;
	}
	
	public void setRanges( Map<Integer, ColorPatternRange> ranges ) {
		this.ranges =ranges;
		
		setDirty( false );
		
        fireTableDataChanged();
	}	
	
	public void setColorServices(ColorService[] colorServices) {
		this.colorServices = colorServices;
	}

	public Map<Integer, ColorPatternRange> getRanges() {
		return ranges;
	}
										  
	public int getColumnCount() {
		return columnNames.length;
	}

	public int getRowCount() {
		return ranges !=null ? ranges.size() : 0;
	}

	public String getColumnName(int col) {
		return columnNames[col];
	}

	public Object getValueAt(int row, int col) {
		Object obj =null;
		
		ColorPatternRange range =(ColorPatternRange)ranges.get( new Integer( row + 1 ) );
		
		switch( col ) {
			case FROM_COL:
				obj =new Double( range.getFrom() );
				break;
				
			case TO_COL:
				obj =new Double( range.getTo() );
				break;
				
			case COLOR_COL:
				obj =new Integer( colorServices[ row ].getIndexForColor( range.getColor() ) );
				break;
			
			case FILL_PATTERN_COL:
				obj =new Integer( range.getFillPattern() );			
				break;
			case CAPTION_COL:
				obj = new String(range.getCaption());
				break;
		}
		
		return obj;
	}
	
    public void setValueAt(Object value, int row, int col) {

		ColorPatternRange range =(ColorPatternRange)ranges.get( new Integer( row + 1 ) );
		
		switch( col ) {
			case FROM_COL:
				range.setFrom( ((Double)value).doubleValue() );
				break;
				
			case TO_COL:
				range.setTo( ((Double)value).doubleValue() );
				break;
				
			case COLOR_COL:
				int idx =((Integer)value).intValue();
				Color color =colorServices[ row ].getColors()[ idx ];
				range.setColor( color );
				break;

			case FILL_PATTERN_COL:
				range.setFillPattern( ((Integer)value).intValue() );
				break;
			case CAPTION_COL:
				range.setCaption( String.valueOf( value ));
				break;
			
		}
    	                    
        fireTableCellUpdated(row, col);
        
        setDirty( true );
    }	

	/*
	 * JTable uses this method to determine the default renderer/
	 * editor for each cell.  If we didn't implement this method,
	 * then the last column would contain text ("true"/"false"),
	 * rather than a check box.
	 */
	@SuppressWarnings("unchecked")
	public Class getColumnClass(int c) {
		return getValueAt(0, c).getClass();
	}

	public boolean isCellEditable(int row, int col) {
		return ( classification == ColorPatternClassification.USER_DEFINED_RANGES 
				|| ( col != FROM_COL && col != TO_COL && col != CAPTION_COL) ); 
	}
     
}
